package cn.zjjd.homeworkserver.util;

/**
 * @author: 小胡
 * @create: 2022/12/19
 * @Description: 引用接口之后的具体操作
 * @FileName: IotSdkClient
 * @Version: 1.0
 */
import com.aliyun.iot20180120.Client;
import com.aliyun.iot20180120.models.PubRequest;
import com.aliyun.iot20180120.models.PubResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.iot20180120.models.*;
import com.aliyun.sdk.service.iot20180120.*;
import com.google.gson.Gson;

import darabonba.core.client.ClientOverrideConfiguration;

import java.util.Base64;
import java.util.concurrent.CompletableFuture;

public class IotSdkClient {
    /**
     * 使用AK&SK初始化Client。
     */
    private static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config();
        config.accessKeyId = accessKeyId;
        config.accessKeySecret = accessKeySecret;
        // 您的可用区ID。
        config.regionId = "cn-shanghai";
        return new Client(config);
    }
    public static void createIotSdkClient() {
        try {
            Client client = createClient("","");
            PubRequest request = new PubRequest()
                    .setIotInstanceId("")
                    .setProductKey("")
                    .setMessageContent(Base64.getEncoder().encodeToString("hello world".getBytes()))
                    .setTopicFullName("")
                    .setQos(0);//目前支持QoS0和QoS1。

            PubResponse response = client.pub(request);
            //获取本次调用requestId。
            System.out.println(response.getBody().getRequestId());
            //调用是否成功。
            System.out.println(response.getBody().getSuccess());

            //调用成功。
            if (response.getBody().getSuccess()) {
                //获取云端消息ID。
                System.out.println(response.getBody().getMessageId());
                //do业务逻辑。
            } else {
                //调用失败。
                //获取调用失败错误码。
                System.out.println(response.getBody().getCode());
                //获取调用失败错误描述。
                System.out.println(response.getBody().getErrorMessage());
            }
        } catch (TeaException error) {
            //服务端业务异常。
            System.out.println(error.getCode());
            System.out.println(error.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void addDevice(String deviceName,String nickName) throws Exception {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("")
                .accessKeySecret("")
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-shanghai") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("iot.cn-shanghai.aliyuncs.com")
                )
                .build();

        RegisterDeviceRequest registerDeviceRequest = RegisterDeviceRequest.builder()
                .iotInstanceId("")
                .productKey("")
                .deviceName(deviceName)
                .nickname(nickName)
                .build();

        CompletableFuture<RegisterDeviceResponse> response = client.registerDevice(registerDeviceRequest);
        RegisterDeviceResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));

        client.close();
    }
    public static void deleteDevice(String deviceName) throws Exception {

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("")
                .accessKeySecret("")
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-shanghai") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("iot.cn-shanghai.aliyuncs.com")
                )
                .build();

        DeleteDeviceRequest deleteDeviceRequest = DeleteDeviceRequest.builder()
                .iotInstanceId("")
                .deviceName(deviceName)
                .productKey("")
                .build();

        CompletableFuture<DeleteDeviceResponse> response = client.deleteDevice(deleteDeviceRequest);
        DeleteDeviceResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
      
        client.close();
    }
    public static String queryDevice() throws Exception{

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("")
                .accessKeySecret("")
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-shanghai") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("iot.cn-shanghai.aliyuncs.com")
                )
                .build();

        QueryDeviceRequest queryDeviceRequest = QueryDeviceRequest.builder()
                .iotInstanceId("")
                .productKey("")
                .pageSize(10)
                .build();

        CompletableFuture<QueryDeviceResponse> response = client.queryDevice(queryDeviceRequest);
        QueryDeviceResponse resp = response.get();

        client.close();
        return new Gson().toJson(resp);
    }
    public static void startdevice(String deviceName) throws Exception {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("")
                .accessKeySecret("")
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-shanghai") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("iot.cn-shanghai.aliyuncs.com")
                )
                .build();

        EnableThingRequest enableThingRequest = EnableThingRequest.builder()
                .iotInstanceId("")
                .productKey("")
                .deviceName(deviceName)
                .build();

        CompletableFuture<EnableThingResponse> response = client.enableThing(enableThingRequest);
        EnableThingResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));

        client.close();
    }
    public static void closedevice(String deviceName) throws Exception {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("")
                .accessKeySecret("")
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-shanghai") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("iot.cn-shanghai.aliyuncs.com")
                )
                .build();

        // Parameter settings for API request
        DisableThingRequest disableThingRequest = DisableThingRequest.builder()
                .iotInstanceId("")
                .productKey("")
                .deviceName(deviceName)
                .build();

        CompletableFuture<DisableThingResponse> response = client.disableThing(disableThingRequest);
        DisableThingResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));

        client.close();
    }
}
